package com.yolo.listener;

import com.alibaba.otter.canal.protocol.CanalEntry;
import com.xpand.starter.canal.annotation.CanalEventListener;
import com.xpand.starter.canal.annotation.ListenPoint;
import com.yolo.config.RabbitMQConfig;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author ChenBo
 * @Date 2020/10/10
 * @Version 1.0
 */
@CanalEventListener
public class GoodsListener {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @ListenPoint(schema = "test",table = "yolo_promotion_track")
    public void goodsListener(CanalEntry.EntryType entryType,
                              CanalEntry.RowData rowData){
        //获取改变之前的数据
        Map<String,String> oldData=new HashMap<>();
        List<CanalEntry.Column> list = rowData.getAfterColumnsList();
        rowData.getBeforeColumnsList().forEach(e->oldData.put(e.getName(),e.getValue()));

        //获取改变之后的数据
        Map<String,String> newData=new HashMap<>();
        rowData.getAfterColumnsList().forEach(e->newData.put(e.getName(),e.getValue()));

        //判断状态改变 发送消息
        if (oldData.get("status").equalsIgnoreCase("false") && newData.get("satus").equalsIgnoreCase("true")){
            rabbitTemplate.convertAndSend(RabbitMQConfig.GOODS_AD_EXCHANGE,"",newData.get("spu_id"));
        }
    }
}
